CodeGear - Where Developers Matter
NEWS

Oct 29, 2008 - HTMLEdit 3.0 Updated

View Changes.

Sep 22, 2008 - HTMLEdit 3.0 Updated

View Changes.

Aug 15, 2008 - HTMLEdit 3.0 Updated

View Changes.

July 18, 2008 - HTMLEdit 3.0 Updated

View Changes.

Images

This article explains what is required of an application using HTMLEdit so that it supports images of any image format (e.g. JPG, PNG, GIF, BMP, etc.).

Document loading

Whenever HTMLEdit loads a document it needs to obtain a graphical representation of each image tag found in the document in order to display the image. Your application must provide a graphical representation - a TGraphic object - for each image tag in the document. The component does not interpret the image source (i.e. the image tag's "src" attribute). It does not, for example, download an image if the image source points to an URL. The application is responsible for interpreting and providing a TGraphic object for each image tag. For that purpose you should implement the OnRequestImage event.

Following is a simple OnRequestImage implementation that enables your application to display BMP images:

procedure Tfrm_Main.HTMLEditRequestImage(iSender:TObject;iImageID,iWidth,iHeight:integer;var oGraphic:TGraphic);
var lBitmap: TBitmap;
begin
  { This event handler interprets the image tag's "src" attribute as the name of
    a BMP file. It attempts to load the bitmap and pass it to HTMLEdit. }
  lBitmap := TBitmap.Create();
  try
    lBitmap.LoadFromFile(HTMLEdit.GetImageUrl(iImageID));
    oGraphic := lBitmap;
  except
    { An error occured while attempting to load the bitmap. Display an error
      graphic instead of the real graphic. }
    FreeAndNil(lBitmap);
    oGraphic := CreateMissingImageBitmap();
  end;
end;

After the component obtained the TGraphic object nothing else is required of the host application. Particularly the host application must not attempt to free the TGraphic object. A reference to the TGraphic object is stored in the component's image cache. When the component no longer needs the image then it automatically frees the associated object.

Image file formats

HTMLEdit supports images of any format, as long as your application is able to provide a TGraphic object. Following is a short list of third-party components for popular image file formats: